package dao;

import dbutil.DBHelp;
import vo.StartCourse;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ssw on 2018/6/16.
 */
public class StartCourseDao {
    public List<StartCourse> askStartCourseList(String uid) {
        DBHelp dh=new DBHelp();
        List<StartCourse> StartCourseList=new LinkedList<>();
        String sql="select scid,cname,lid,clname,scroom,sctime,sccredit\n" +
                "from sc,course\n" +
                "where course.cid=sc.cid and lid=?;";
        try {
            ResultSet rs=dh.query(sql,uid);
            while(rs.next()){
                StartCourseList.add(new StartCourse(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getInt(7)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return StartCourseList;
    }

    public boolean addsc(String cno, String lid, String clname, String scroom, String sctime, String sccredit) {
        int temp=-1;
        DBHelp dh=new DBHelp();
        String sql="insert into sc(cid,lid,clname,scroom,sctime,sccredit) values(?,?,?,?,?,?)";
        try {
            temp=dh.insert(sql,cno,lid,clname,scroom,sctime,sccredit);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        return temp>0;
    }

    public List<StartCourse> askAllStartCourseList() {
        DBHelp dh=new DBHelp();
        List<StartCourse> StartCourseList=new LinkedList<>();
        String sql="select scid,cname,lid,sc.clname,scroom,sctime,sccredit\n" +
                "from sc,course\n" +
                "where course.cid=sc.cid";
        try {
            ResultSet rs=dh.query(sql);
            while(rs.next()){
                StartCourseList.add(new StartCourse(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getInt(7)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return StartCourseList;
    }

    public List<StartCourse> askXK(String uid) {
        DBHelp dh=new DBHelp();
        List<StartCourse> StartCourseList=new LinkedList<>();
        String sql="select scid,cname,lid,sc.clname,scroom,sctime,sccredit,students.sid\n" +
                "        from sc,course,students\n" +
                "        where students.clname=sc.clname and sc.cid=course.cid and students.sid=?";
        try {
            ResultSet rs=dh.query(sql,uid);
            while(rs.next()){
                StartCourseList.add(new StartCourse(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getInt(7)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return StartCourseList;



    }
}
